package com.example.myfirstapplication.provider

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.net.Uri
import com.example.myfirstapplication.dbHelper.MyDatabaseHelper

class DatabaseProvider : ContentProvider() {

    private val studentTable = 0
    private val studentItem = 1
    private val scoreTable = 2
    private val scoreItem = 3
    private val authority = "com.example.myfirstapplication"
    private var dbHelper: MyDatabaseHelper? = null
    private val uriMatcher = UriMatcher(UriMatcher.NO_MATCH)

    init {
        uriMatcher.addURI(authority, "table1", studentTable)
        uriMatcher.addURI(authority, "table1/#", studentItem)
        uriMatcher.addURI(authority, "table2", scoreTable)
        uriMatcher.addURI(authority, "table2/#", scoreItem)
    }

    override fun insert(uri: Uri, values: ContentValues?) = dbHelper?.let {
        val db = it.writableDatabase
        val uriReturn = when (uriMatcher.match(uri)) {
            studentTable, studentItem -> {
                val studentId = db.insert("student", null, values)
                Uri.parse("content://$authority/student/$studentId")
            }
            scoreTable, scoreItem -> {
                val scoreId = db.insert("score", null, values)
                Uri.parse("content://$authority/score/$scoreId")
            }
            else -> null
        }
        uriReturn
    }

    override fun delete(
        uri: Uri,
        selection: String?,
        selectionArgs: Array<out String>?
    ) = dbHelper?.let {
        val db = it.writableDatabase
        val deleteRows = when (uriMatcher.match(uri)) {
            studentTable -> {
                db.delete("student", selection, selectionArgs)
            }
            studentItem -> {
                val studentId = uri.pathSegments[1]
                db.delete("student", "id=?", arrayOf(studentId))
            }
            scoreTable -> {
                db.delete("student", selection, selectionArgs)
            }
            scoreItem -> {
                val scoreId = uri.pathSegments[1]
                db.delete("score", "id=?", arrayOf(scoreId))
            }
            else -> 0
        }
        deleteRows
    } ?: 0

    override fun update(
        uri: Uri, values: ContentValues?, selection: String?,
        selectionArgs: Array<out String>?
    ) = dbHelper?.let {
        val db = it.writableDatabase
        val updateRows = when (uriMatcher.match(uri)) {
            studentTable -> {
                db.update("student", values, selection, selectionArgs)
            }
            studentItem -> {
                val studentId = uri.pathSegments[1]
                db.update("student", values, "id=?", arrayOf(studentId))
            }
            scoreTable -> {
                db.update("student", values, selection, selectionArgs)
            }
            scoreItem -> {
                val scoreId = uri.pathSegments[1]
                db.update("score", values, "id=?", arrayOf(scoreId))
            }
            else -> 0
        }
        updateRows
    } ?: 0

    override fun getType(uri: Uri) = when (uriMatcher.match(uri)) {
        studentTable -> "vnd.android.cursor.dir/vnd.com.example.myfirstapplication.student"
        studentItem -> "vnd.android.cursor.item/vnd.com.example.myfirstapplication.student"
        scoreTable -> "vnd.android.cursor.dir/vnd.com.example.myfirstapplication.score"
        scoreItem -> "vnd.android.cursor.item/vnd.com.example.myfirstapplication.score"
        else -> null
    }

    override fun query(
        uri: Uri, projection: Array<String>?, selection: String?,
        selectionArgs: Array<String>?, sortOrder: String?
    ) = dbHelper?.let {
        val db = it.readableDatabase
        val cursor = when (uriMatcher.match(uri)) {
            studentTable -> {
                db.query("student", projection, selection, selectionArgs, null, null, sortOrder)
            }
            studentItem -> {
                val studentId = uri.pathSegments[1]
                db.query("student", projection, "id=?", arrayOf(studentId), null, null, sortOrder)
            }
            scoreTable -> {
                db.query("score", projection, selection, selectionArgs, null, null, sortOrder)
            }
            scoreItem -> {
                val scoreId = uri.pathSegments[1]
                db.query("score", projection, "id=?", arrayOf(scoreId), null, null, sortOrder)
            }
            else -> null
        }
        cursor
    }

    override fun onCreate() = context?.let {
        dbHelper = MyDatabaseHelper(it, "student.db", 4)
        true
    } ?: false
}